//
//  TopicHeaderDetaillView.m
//  Protect -A
//
//  Created by 付剑英 on 16/6/28.
//  Copyright © 2016年 付剑英. All rights reserved.
//

#import "TopicHeaderDetaillView.h"
#import "baseLabel.h"
#import "baseImageView.h"
#import  <Masonry.h>
#import  <UIImageView+WebCache.h>

@interface TopicHeaderDetaillView ()
@property (nonatomic, retain)baseLabel  *labelMoreTopic;  /**< 更多话题 */

@property (nonatomic, retain) BaseView *ViewBackGround; /**< 白色背景 */
@property (nonatomic, retain) baseLabel *labelType; /**< 类型  */
@property (nonatomic, retain) baseLabel *labelContent; /**< 详情内容 */

@property (nonatomic, retain) baseImageView *imgPhoto; /**< 照片 */

@property (nonatomic, retain) baseLabel *labelUserNameTime; /**< 时间 */
@property (nonatomic, retain) baseLabel *labelVisitCount; /**< 浏览 人数 */


@property (nonatomic, retain) baseLabel *labelHotComment; /**< 热门评论 */
@end

@implementation TopicHeaderDetaillView

#pragma mark - ------------ 内存 管理 ---------------
- (void)dealloc
{
    [_labelMoreTopic release];
    [_ViewBackGround release];
    [_labelType release];
    [_labelContent release];
    [_imgPhoto release];
    [_labelUserNameTime release];
    [_labelVisitCount release];
    [_labelHotComment release];
    [_model release];
    [super dealloc];
    
}


#pragma mark - ------------ 初始化  ---------------
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self createSubViews];
    }
    return self;
}

#pragma mark - ------------ 创建子控件 ---------------
- (void)createSubViews
{
    
    
    self.labelMoreTopic = [[baseLabel alloc]init];
    [self addSubview:_labelMoreTopic];
    _labelMoreTopic.textColor = [UIColor grayColor];
    _labelMoreTopic.font = [UIFont systemFontOfSize:13];
    
    
    self.ViewBackGround = [[BaseView alloc]init];
    [self addSubview:_ViewBackGround];
    /** 夜间 */
//    _ViewBackGround.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor],[UIColor grayColor]);
    
    
    self.labelType = [[baseLabel alloc]init];
    [self.ViewBackGround addSubview:_labelType];
    _labelType.textAlignment = NSTextAlignmentCenter;
    _labelType.font = [UIFont systemFontOfSize:15];
    _labelType.textColor = [UIColor colorWithRed:0.00 green:0.61 blue:0.84 alpha:1.00];
    
    
    self.labelContent = [[baseLabel alloc]init];
    [self.ViewBackGround addSubview:_labelContent];
    _labelContent.numberOfLines = 0;
    _labelContent.font = [UIFont systemFontOfSize:15];
    /** 夜间 */
//    _labelContent.dk_textColorPicker = DKColorWithColors([UIColor grayColor],[UIColor lightGrayColor]);
    
    
    self.imgPhoto =  [[baseImageView alloc]init];
    [self.ViewBackGround addSubview:_imgPhoto];
    _imgPhoto.contentMode = UIViewContentModeScaleAspectFill;
    _imgPhoto.clipsToBounds = YES;
    
    
    self.labelUserNameTime = [[baseLabel alloc]init];
    [self.ViewBackGround addSubview:_labelUserNameTime];
    _labelUserNameTime.textColor = [UIColor lightGrayColor];
    _labelUserNameTime.font = [UIFont systemFontOfSize:13];
    
    
    self.labelVisitCount = [[baseLabel alloc]init];
    [self.ViewBackGround addSubview:_labelVisitCount];
    _labelVisitCount.textAlignment = NSTextAlignmentRight;
    _labelVisitCount.font = [UIFont systemFontOfSize:13];
    _labelVisitCount.textColor = [UIColor lightGrayColor];
    
    
    self.labelHotComment = [[baseLabel alloc]init];
    [self addSubview:_labelHotComment];
    _labelHotComment.textAlignment = NSTextAlignmentCenter;
    _labelHotComment.textColor = [UIColor grayColor];
    
    
    _labelType.backgroundColor = [UIColor colorWithRed:0.89 green:0.95 blue:0.98 alpha:1.00];
    
    [_labelMoreTopic release];
    [_ViewBackGround release];
    [_labelType release];
    [_labelContent release];
    [_imgPhoto release];
    [_labelUserNameTime release];
    [_labelVisitCount release];
    [_labelHotComment release];
    
    
    //    _labelMoreTopic.backgroundColor = [UIColor redColor];
    //    _labelContent.backgroundColor = [UIColor orangeColor];
    //    _imgPhoto.backgroundColor = [UIColor blueColor];
    //    _labelUserNameTime.backgroundColor = [UIColor redColor];
    //    _labelVisitCount.backgroundColor = [UIColor redColor];
    //    _labelHotComment.backgroundColor = [UIColor redColor];
}

#pragma mark - ------------ 代码约束 文字自适应 判断 有无图片 ---------------
- (void)layoutSubviews
{
    
    [super layoutSubviews];

    
    [_labelMoreTopic mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self).offset(10);
        make.top.mas_equalTo(self).offset(15);
        make.right.mas_equalTo(self).offset(-10);
        make.height.mas_equalTo(30);
    }];
    
    CGFloat h = [[self class]heightWithText:_model.content];
    
    if (_model.path != nil) {
        
        
        [_ViewBackGround mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.left.right.mas_equalTo(self).offset(0);
            make.height.mas_equalTo(h + 250);
            make.top.mas_equalTo(_labelMoreTopic.mas_bottom).offset(10);
        }];
        
        [_labelType mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.left.mas_equalTo(_ViewBackGround).offset(10);
            make.width.mas_equalTo(50);
            make.height.mas_equalTo(30);
        }];
        
        
        [_labelContent mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(_labelType);
            make.top.mas_equalTo(_labelType.mas_bottom).offset(10);
            make.right.mas_equalTo(_ViewBackGround).offset(-10);
            make.height.mas_equalTo(h);
        }];
        
        _imgPhoto.hidden = NO;
        [_imgPhoto mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(_labelType);
            make.top.mas_equalTo(_labelContent.mas_bottom).offset(10);
            make.width.mas_equalTo(120);
            make.height.mas_equalTo(140);
        }];
        
        [_labelUserNameTime mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(_labelType);
            make.top.mas_equalTo(_imgPhoto.mas_bottom).offset(10);
            make.width.mas_equalTo(200);
            make.height.mas_equalTo(30);
        }];
        
        [_labelVisitCount mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_labelUserNameTime);
            make.right.mas_equalTo(_ViewBackGround).offset(-10);
            make.left.mas_equalTo(_labelUserNameTime.mas_right).offset(10);
            make.height.mas_equalTo(_labelUserNameTime);
            
        }];
        
        [_labelHotComment mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_ViewBackGround.mas_bottom).offset(10);
            make.left.mas_equalTo(self).offset(10);
            make.right.mas_equalTo(self).offset(-10);
            make.height.mas_equalTo(30);
            
        }];
        
    }else if (_model.path == nil){
        
        
        [_ViewBackGround mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.left.right.mas_equalTo(self).offset(0);
            make.height.mas_equalTo(h + 100);
            make.top.mas_equalTo(_labelMoreTopic.mas_bottom).offset(10);
        }];
        
        [_labelType mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.left.mas_equalTo(_ViewBackGround).offset(10);
            make.width.mas_equalTo(50);
            make.height.mas_equalTo(30);
        }];
        
        
        [_labelContent mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(_labelType);
            make.top.mas_equalTo(_labelType.mas_bottom).offset(10);
            make.right.mas_equalTo(_ViewBackGround).offset(-10);
            make.height.mas_equalTo(h);
        }];
        
        _imgPhoto.hidden = YES;
        
        [_labelUserNameTime mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(_labelType);
            make.top.mas_equalTo(_labelContent.mas_bottom).offset(10);
            make.width.mas_equalTo(200);
            make.height.mas_equalTo(30);
        }];
        
        [_labelVisitCount mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_labelUserNameTime);
            make.right.mas_equalTo(_ViewBackGround).offset(-10);
            make.left.mas_equalTo(_labelUserNameTime.mas_right).offset(10);
            make.height.mas_equalTo(_labelUserNameTime);
            
        }];
        
        [_labelHotComment mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_ViewBackGround.mas_bottom).offset(10);
            make.left.mas_equalTo(self).offset(10);
            make.right.mas_equalTo(self).offset(-10);
            make.height.mas_equalTo(30);
            
        }];
        
        
    }
    
}


#pragma mark - ------------ model赋值 ---------------
- (void)setModel:(TopicModel *)model
{
    
    if (_model != model) {
        [_model release];
        _model = [model retain];
    }
    
    //设置高亮字体 来自 ** 更多话题
    _labelMoreTopic.text = [NSString stringWithFormat:@"来自 %@ 更多话题", model.clubname];
    NSMutableAttributedString *strTop = [[NSMutableAttributedString alloc]initWithString:_labelMoreTopic.text];
    NSInteger length = [model.clubname length];
    [strTop addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:14] range:NSMakeRange(3, length)];
    
    [strTop addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(3, length)];
    
    _labelMoreTopic.attributedText = strTop;
    [strTop release];
    
    
    //精选
    _labelType.text = model.tagName;
    
    //详情内容
    _labelContent.text = model.content;
    
    //照片
    [_imgPhoto sd_setImageWithURL:[NSURL URLWithString:model.path]];
    
    
    //用户名高亮字变蓝 . 时间
    NSString *modelTime =  [[self class]howlongToCurrntTime:model.active_time.stringValue];
    _labelUserNameTime.text = [NSString stringWithFormat:@"%@ · %@", model.username, modelTime];
    
    NSMutableAttributedString *strTime = [[NSMutableAttributedString alloc]initWithString:_labelUserNameTime.text];
    NSInteger length1 = [model.username length];
    
    [strTime addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:0.00 green:0.61 blue:0.84 alpha:1.00] range:NSMakeRange(0, length1)];
    _labelUserNameTime.attributedText = strTime;
    
    [strTime release];
    
    //浏览人数
    if (model.visit_count != nil) {
        
        _labelVisitCount.text = [@"浏览" stringByAppendingString: model.visit_count.stringValue];
    }
    
    //热门评论
    _labelHotComment.text = @"—————— 热门评论 ——————";
    
}



#pragma mark - ------------ 文本高度自适应 ---------------
+ (CGFloat)heightWithText:(NSString *)text
{
    
    CGRect rect = [text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 20, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:15]} context:nil];
    
    return rect.size.height;
    
}

#pragma mark - ------------ 时间转换 ---------------
+ (NSString *)howlongToCurrntTime:(NSString *)time
{
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:time.doubleValue];
    
    NSDateFormatter *dateFomatter = [[NSDateFormatter alloc]init];
    [dateFomatter setDateFormat:@"M月dd日 HH:mm"];
    
    //1970  转至今的时间
    NSString *fomatterString = [dateFomatter stringFromDate:date];
    
    [dateFomatter release];
    
    return fomatterString;
    
    
}

@end
